package Operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class DelOperation implements IOOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入你要删除图书的名称:");
        Scanner in = new Scanner(System.in);
        String name = in.nextLine();
        int currentSize = bookList.getUsedSize();
        int pos = -1;

        int i = 0;
        for (; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                pos = i;
                break;
            }
        }

        if (i == currentSize) {
            System.out.println("没有图书删除！");
            return;
        }else {
            int j = pos;
            for (; i < currentSize - 1; j++) {
                Book book = bookList.getBook(j+1);
                bookList.setBook(j,book);
            }
            bookList.setBook(j,null);
            bookList.setUsedSize(currentSize-1);
        }
    }
}
